package main

import (
	"database/sql"
	"fmt"

	_ "github.com/go-sql-driver/mysql"
)

type rowCols struct {
	Id        int    `json:"id" db:"id"`
	Unique_id string `json:"unique_id" db:"unique_id"`
}

func UpdatePurModel() {
	// 2. 创建数据库连接池
	db, err := sql.Open("mysql", "dcroot:WEVkyOHPFjOyvbSGXU2fwVHXcoVs6Val@tcp(dc03.iotdreamcatcher.net.cn:5306)/dc_v2_factory?charset=utf8")
	if err != nil {
		panic(err)
	}
	// 3. 连接数据库
	defer db.Close()
	// 4. 执行sql语句
	// 4.1 查询sql
	sql := "select id,unique_id from air_purifier"
	rows, err := db.Query(sql)
	if err != nil {
		panic(err)
	}
	// 4.2 获取结果
	// 4.2.1 获取结果中的列名
	//var row Purifier
	var row rowCols
	for rows.Next() {
		//err = rows.Scan(&row.Id, &row.Created_at, &row.Updated_at, &row.Deleted_at, &row.Encode_code_str, &row.Unique_id, &row.Secert_key, &row.Batch_number, &row.Deadline, &row.Model)
		err = rows.Scan(&row.Id, &row.Unique_id)
		if err != nil {
			panic(err)
		}
		//fmt.Println(row)
		var sql string = ""
		switch row.Unique_id[0:2] {
		case "A2":
			// 根据id更新model
			sql = "update air_purifier set model = 'A20026' where id = ?"
		case "A3":
			sql = "update air_purifier set model = 'A30013' where id = ?"
		case "C2":
			sql = "update air_purifier set model = 'C20058' where id = ?"
		case "B2":
			sql = "update air_purifier set model = 'B20058' where id = ?"
		}
		_, err := db.Exec(sql, row.Id)
		//fmt.Println("=========row", row.Id)
		//fmt.Println("==========", sql)
		if err != nil {
			panic(err)
		}
	}
	fmt.Println("exec success")
}

func main() {
	UpdatePurModel()
}
